package com.hundsun.documentripper.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hundsun.documentripper.domain.pojo.Product;
import com.hundsun.documentripper.domain.pojo.ProductVersion;
import com.hundsun.documentripper.domain.pojo.UserProject;
import com.hundsun.documentripper.domain.query.ProductQuery;
import com.hundsun.documentripper.error.BaseException;
import com.hundsun.documentripper.mapper.ProductMapper;
import com.hundsun.documentripper.mapper.UserProductMapper;
import com.hundsun.documentripper.service.ProductService;
import com.hundsun.documentripper.service.ProductVersionService;
import com.hundsun.documentripper.utils.StringUtil;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：产品接口实现
 * @date ：Created in 2021/1/26
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private UserProductMapper userProductMapper;
    @Autowired
    private ProductVersionService productVersionService;

    @Override
    public void insertProduct(Product product) {
        // 判断用户编号是否已经存在
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("code", product.getCode());
        Product example = productMapper.selectOne(wrapper);
        if (null != example) {
            throw new BaseException("2002");
        }
        product.setUuid(UUID.randomUUID().toString());
        productMapper.insert(product);
        // 创建者默认拥有权限
        userBindProduct(product.getUuid(), product.getCreateUser());
    }

    @Override
    public void updateProduct(Product product) {
        productMapper.updateById(product);
    }

    @Override
    public Product getProductById(String uuid) {
        return productMapper.selectById(uuid);
    }

    @Override
    public IPage<Product> getProductListByPage(ProductQuery productQuery) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        wrapper.like(StringUtil.isNotEmpty(productQuery.getUuid()), "uuid", productQuery.getUuid());
        wrapper.like(StringUtil.isNotEmpty(productQuery.getName()), "name", productQuery.getName());
        wrapper.inSql("uuid",
                "select distinct product_uuid from user_product where user_uuid= '" + productQuery.getUserUuid() + "'");
        return productMapper.selectPageByUser(new Page<Product>(productQuery.getCurrent(), productQuery.getSize()), wrapper);
    }

    @Override
    public void deleteProduct(String uuid) {
        // 删除产品相关版本
        List<ProductVersion> productVersionList = productVersionService.getAllProductVersion(uuid, false);
        for (ProductVersion productVersion : productVersionList) {
            // 循环删除全部版本
            productVersionService.deleteProductVersion(productVersion.getUuid());
        }
        productMapper.deleteById(uuid);
        // 删除产品分配的用户的数据
        QueryWrapper<UserProject> wrapper = new QueryWrapper<>();
        wrapper.eq("product_uuid", uuid);
        userProductMapper.delete(wrapper);
    }

    @Override
    public void userBindProduct(String uuid, String userUuid) {
        userProductMapper.insert(UserProject.builder().productUuid(uuid).userUuid(userUuid).build());
    }

    @Override
    public void userUnbindProduct(String uuid, String userUuid) {
        QueryWrapper<UserProject> wrapper = new QueryWrapper<>();
        wrapper.eq("product_uuid", uuid);
        wrapper.eq("user_uuid", userUuid);
        userProductMapper.delete(wrapper);
    }

}